var request = require('request');
var fs = require('fs');
var iconvLite = require('iconv-lite');
var REG = new RegExp(/charset=(.*)/);
var defaultCharSet = 'utf8';
console.log('request begin...');
var baseHost = 'http://www.08ggg.com';
request('http://www.08ggg.com/htm/movielist5/',{encoding:null},function(error, response, body){

    if(error){
        throw  error;
    }else{
        var charset = 'utf8';
        var temp = null;
        if(temp = REG.exec(response.headers['content-type'])){
            charset = temp[1];
        }
        if(charset === "gbk" || charset === "GBK" || charset === "gb2312" || charset === "GB2312"){
            defaultCharSet = 'gbk';
        }else if(charset === 'utf8' || charset === 'utf-8' || charset ==='UTF8' || charset === 'UTF-8'){
            defaultCharSet = 'utf8';
        }
        body = iconvLite.decode(body,defaultCharSet);
        var REGTEST = new RegExp(/<div class=pic>([\s\S]*?)<div class=txt>/gi);
        var tempArray = [];
//        var tempResult = REGTEST.exec(body);
//        console.log('总共有：'+tempResult.length);
        var next = null;
        while (next = REGTEST.exec(body)) {
            tempArray.push(next[1]);
        }
        var itemArray = [];
        var tempString = '';
        for(var i = 0 ; i< tempArray.length ; i++){
            console.log('正在保存第'+(1+i)+'条链接地址！');
            var item = baseHost+tempArray[i].match(/<A href="(.*?)"/)[1];
            tempString += item+'\r\n';
            itemArray.push(item);
        }
        var subArray = [];
        for(i = 0 ; i < itemArray.length ; i++){
            request(itemArray[i],{encoding:null},function(err,res,body){
                body = iconvLite.decode(body,defaultCharSet);
                var subREG = body.match(/<a class="new" target="_blank" href="(.*?)"/)[1];
                request(baseHost+subREG,{encoding:null},function(err,res,body){
                    body = iconvLite.decode(body,defaultCharSet);
                    var sub2REG = body.match(/<param name="URL" value="(.*?)"/)[1];
                    var subStr = sub2REG + '\r\n';
                    fs.appendFile('result.txt',subStr,'utf8',function(err){
                        if(err){
                            throw err;
                        }else{
                            console.log('AV 地址保存成功！');
                        }
                    });
                });
                subArray.push(baseHost+subREG);
                //console.log(subArray);
            });
        }
//        fs.writeFile('data.txt', tempString, function (err) {
//            if (err) throw err;
//            console.log('The "data to append" was appended to file!');
//        });
//        console.log(itemArray);
//        fs.writeFile('taobao.html',body,function(err){
//            if(err){
//                throw  err;
//            }else{
//                console.log('Html is saved!');
//            }
//        })
    }
    console.log('request end!');
});